<?php

namespace App\Services\Collection;

use App\Exceptions\CollectionException;
use App\Helpers\FileHelper;
use Exception;

class CollectionFileService extends BaseCollectionService
{
    /**
     * 添加藏品文件
     *
     * @return void
     * @throws Exception
     */
    public function create(array $params)
    {
        // 查询藏品是否存在
        $collection = $this->getCollectionModel()->getItemById($params['collectionId']);
        if (empty($collection)) {
            throw new CollectionException('藏品不存在', CollectionException::CODE_1001);
        }

        $addData = [
            'collection_id' => $params['collectionId'],
            'type'          => $params['type'],
            'file_url'      => $params['fileUrl'],
        ];

        // 添加藏品文件
        $this->getCollectionFilesModel()->addItem($addData);
    }

    /**
     * 更新藏品文件
     *
     * @param array $params
     * @return void
     * @throws CollectionException
     * @throws Exception
     */
    public function update(array $params)
    {
        // 查询藏品文件是否存在
        $collection = $this->getCollectionFilesModel()->getItemById($params['id']);
        if (empty($collection)) {
            throw new CollectionException('藏品文件不存在', CollectionException::CODE_1002);
        }

        $updateData = [
            'type'     => $params['type'],
            'file_url' => $params['fileUrl'],
        ];

        // 更新藏品文件
        $this->getCollectionFilesModel()->updateItemById($params['id'], $updateData);
    }

    /**
     * 删除藏品文件
     *
     * @return void
     * @throws Exception
     */
    public function delete(array $params)
    {
        // 查询藏品文件是否存在
        $collection = $this->getCollectionFilesModel()->getItemById($params['id']);
        if (empty($collection)) {
            throw new CollectionException('藏品文件不存在', CollectionException::CODE_1002);
        }

        // 删除藏品文件
        $this->getCollectionFilesModel()->deleteItemById($params['id']);
    }

    /**
     * 批量删除藏品文件
     *
     * @return void
     * @throws Exception
     */
    public function deleteByIds(array $params)
    {
        // 查询藏品文件是否存在
        $collectionList = $this->getCollectionFilesModel()->getItemsByIds($params['ids']);
        if (empty($collectionList)) {
            throw new CollectionException('藏品文件不存在', CollectionException::CODE_1002);
        }

        // 需要删除的藏品文件
        $collectionIds = array_intersect(array_column($collectionList, 'id'), $params['ids']);

        // 删除藏品文件
        $this->getCollectionFilesModel()->deleteItemsByIds($collectionIds);
    }

    /**
     * 藏品文件列表
     *
     * @param array $params
     * @return array
     * @throws CollectionException
     * @throws Exception
     */
    public function list(array $params): array
    {
        // 查询藏品是否存在
        $collection = $this->getCollectionModel()->getItemById($params['collectionId']);
        if (empty($collection)) {
            throw new CollectionException('藏品不存在', CollectionException::CODE_1001);
        }

        // 查询藏品文件列表
        $collectionFileList = $this->getCollectionFilesModel()->getListByCollectionId($params['collectionId'], [
            'id', 'type', 'file_url',
        ]);

        $list = [];
        foreach ($collectionFileList as &$item) {
            $list[] = [
                'id'      => $item['id'],
                'type'    => $item['type'],
                'fileUrl' => FileHelper::getCompleteUrl($item['file_url']),
            ];
        }

        return [
            'list' => $list,
        ];
    }

    /**
     * 藏品文件详情
     *
     * @param array $params
     * @return array
     * @throws CollectionException
     * @throws Exception
     */
    public function detail(array $params): array
    {
        // 查询藏品是否存在
        $collection = $this->getCollectionFilesModel()->getItemById($params['id'], [
            'id', 'type', 'file_url',
        ]);
        if (empty($collection)) {
            throw new CollectionException('藏品文件不存在', CollectionException::CODE_1002);
        }

        return [
            'id'       => $collection['id'],
            'type'     => $collection['type'],
            'fileUrl'  => FileHelper::getCompleteUrl($collection['file_url']),
            'filename' => $collection['file_url'],
        ];
    }

}
